//素金
var APP=is_app();
var app_url=location.origin;
 var TEAM=0;
 var PlaceOrder=false;//代下单
(function () {
    var products = {
        init: function () {
        	args = GetUrlParms();				
			if(args["team"]!=undefined){						
				 TEAM = args["team"];//产品ID					
			} 
			if(args["cid"]!=undefined){						
				 Customer_id = args["cid"];	
				 PlaceOrder=true;//代下单
			} 
            this.bindEvents();
            this.loadPrimeGolds();
        },
        bindEvents: function () { 
            var self = this;
             //筛选
            $("#term_app_btn").on("click",function(){
            	if(PlaceOrder==true){
            		location.href="/Customer/PrimeGoldFilter?team=3&cid="+Customer_id;
            	}else{
            		location.href="/Customer/PrimeGoldFilter?team="+TEAM;
            	}
            });
           //app
           $("#prime_golds").on("click","#app_link_item",function(){
           		var link_url=$(this).attr("data-src");
           		if(APP){
           			var appJson={};
           			appJson.title="素金详情";
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
           		}else{
           			location.href=link_url;
           		}       
           });
            //点击菜单
			$("#show_menu_btn").on("click",function(){
				$("#assistant_menu_box").toggle();
			});
			//关闭菜单
			$("#assistant_menu_box").on("click",function(){
				$("#assistant_menu_box").hide();
			});
			//新增
			$("#add_btn").on("click",function(){
				var link_url="/Team/PrimeGoldManagement?type=6";
				if(APP){
					var appJson={};
           			appJson.title="素金新增";
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
				}else{
					location.href=link_url;
				}
			});
        },

        //加载数据
        loadPrimeGolds: function () {
            var self = this;
            myApp.showIndicator();//显示加载中
            var params = PARAMS;//APP手机过来的参数
         	if(params==null){
         		params={};
         	}
         	if(TEAM==2){
         		params.IsMyStock=true;//现货
         	}else{
         		params.OnShelves = "1"; 
         	}
         	if(TEAM==0){
         		params.CustomerID = CustomerID;//客户ID
         	}
            params.useDict = true;
            var params = JSON.stringify(params);
            var url ="/Team/GetPrimeGoldsFilters";
            $.ajax({
                url: url,
                data: params,
                type: "POST",
                contentType: "application/json; charset=utf8",
                success: function (result) {
                    $("#prime_golds").html('');
                    tempSupplier = 0;
                    var res=eval('(' + result + ')')
                    if (res.msgcode == true) {
                        window.GOODS_ITEMS = res.rows;
                        var dtRenderStart = new Date().getTime();
                        isSearching = false;
                        if (res.msginfo.rows.length == 0) {
                            $("#prime_golds").html('<div class="no_data_tip">没有符合条件的结果</div>');
                        } else {
                            var prime_golds = [];
                            for (var i = 0; i < res.msginfo.rows.length; i++) {
                                var row = res.msginfo.rows[i];
                                prime_golds.push(
                                        '<li data="', row.Supplier, '" itemtype="" itemid="', row.ID, '" class="',i%2==0?"":"mark",'">',
                                        	'<a data-src=', "/", TEAM > 0 ? "Team" : "Customer", "/PrimeGoldDetails/?ID=", row.ID, "&team=", TEAM,PlaceOrder?"&cid="+Customer_id:"",' id="app_link_item" data-type="素金">',
                                        	'<div id="image_app_box" class="image_app_box">',
                                        		'<img class="product_img lazy lazy-fadein" data-src="', imgSrc(row.Images), '" src="#" alt="彩宝"/>',
                                        	'</div>',
                                        	'<div class="product_data">',
		                                		'<h4>',
		                                			returnPrice(row),
		                                			'<span><b title="所在地">',row.Country,'</b>',
		                                		'</h4>',//<span><b>',_(placeText(row.Place)),'</b></span>
		                                		'<div class="data_box">',
		                               				'<div class="series">',row.Name,'</div>',
		                                			'<div>',
		                                				'<b>',GetText("prime_golds",1,row.Weight,"Weight"),'</b>',
		                                				'<b>' ,GetText("prime_golds",1,row.Type,"Type"),'</b>',
		                               					'<b>',GetText("prime_golds",1,row.Style,"Style"),'</b>',
		                               					'<b>',GetText("prime_golds",1,row.IsStock,"IsStock"),'</b>',
		                               				'</div>',
//		                                			'<div class="supplier_box">',
//		                                				'<b title="供应商">',ConvertSupplier(row),'</b>',
//		                                			'</div>',
		                                		'</div>',
		                                	'</div>',
		                                '</a></li>'
                                     );
                            }
                            $("#prime_golds").html(prime_golds.join(''));
                            //懒加载
                             var n = 0,
						     imgNum = $("#prime_golds li").length,
						     img = $('#prime_golds li .product_img');
						     myApp.initImagesLazyLoad(img)
						   	$$('img.lazy').trigger('lazy');
//						    lazyload();
//						    $(window).scroll(lazyload);
//						    function lazyload(event) {
//						        for (var i = n; i < imgNum; i++) {
//						            if (img.eq(i).offset().top < parseInt($(window).height()) + parseInt($(window).scrollTop())) {
//						                if (img.eq(i).attr("src") == "#") {
//						                    var src = img.eq(i).attr("data-src");
//						                    img.eq(i).attr("src", src);
//						                    n = i + 1;
//						                }
//						            }
//						        }
//						    }
                            //图片加载出来时居中显示
		                    $(".image_app_box .product_img").load(function(){
		                    	var w=$(this).width();
		                    	var h=$(this).height();
		                    	var docu_w=$(document).width();
		                    	if(docu_w>760){
		                    		var screenW=244;
		                    	}else{
		                    		var screenW=docu_w/2;
		                    	}
		                    	if(w>h){
		                    		var w_scale=w/screenW;
		                    		var now_h=h/w_scale;
		                    		var pa_top=screenW/2-now_h/2;
		                    		$(this).css({
		                    			"width":"100%",
		                    			"padding-top":pa_top+"px",
		                    			"padding-bottom":pa_top+"px"
		                    			});
		                    	}else if(h>w){
		                    		var h_scale=h/screenW;
		                    		var now_w=w/h_scale;
		                    		var pa_left=screenW/2-now_w;
		                    		$(this).css({
		                    			"height":"100%",
		                    			"padding-left":pa_left/2+"px",
		                    			"padding-right":pa_left/2+"px"
		                    		});
		                    	}else{
		                    		$(this).css({
		                    			"height":"100%",
		                    			"width":"100%"
		                    		});
		                    	}
		                    });
                        }

                        //console.info(new Date().getTime() - dtRenderStart + "ms");
                    } else {
                    	myApp.alert(res.msginfo);                      
                    }
                    myApp.hideIndicator();//隐藏加载中
                }
            });
        },

    };
    products.init();
})();
//没权限返回的价格
function returnPrice(data){
	//if(data.Price || data.Price==0){
		return '<span>￥<b title="价格">'+formatRMBPrice(data.Price)+'</b></span>';		
	//}else{
	//	return '<span style="font-size:14px;">￥<b title="价格">'+('权限不足')+'</b></span>';	
	//}
}

//优先显示简称
function ConvertSupplier(data) {
//	if(data.Supplier != undefined) {
//		if(data.ShortName != "") {
//			return data.ShortName;
//		} else {
			return data.Supplier;
		//}
//	} else
//		return "-";
}

//返回图片链接
function imgSrc(images) {
    if (images) {
        images = images.split('|');
        var small = images[0];
        var big = small.replace('400x400', '1920x1920');
        return small;
    }
    return '../images/no_img.jpg';//无图时返回默认图片
}
